<?php
/**
 * Created by PhpStorm.
 * User: kun
 * Date: 26/6/19
 * Time: 18:20
 */

namespace app\api\controller\v1;


use app\api\controller\Common;
use app\common\lib\Aes;
use app\common\lib\exception\ApiException;


class Comment extends AuthBase
{
    /**
     * 评论---回复功能接口
     */
    public function save()
    {
        $data = input('post.', []);
        $data['content'] = htmlspecialchars(input('post.content'));

        if (empty($data['news_id'])) {
            return show(config('code.error'), 'id不存在', [], 403);
        }
        try {
            $news = model('News')->get(['id' => $data['news_id'], 'status' => config('code.app_status_success')]);
        } catch (\Exception $e) {
            return show(config('code.error'), $e->getMessage(), [], 403);
        }

        if (is_null($news)) {
            return show(config('code.error'), '文章不存在', [], 403);
        }


        $validate = validate('Comment');

        if (!$validate->check($data)) {
            return $this->error($validate->getError());
        }

        $data['user_id'] = $this->user->id;
        try {
            $commentId = model('Comment')->add($data);
            if ($commentId) {
                return show(config('code.success'), 'ok', [], 202);
            } else {
                return show(config('code.error'), '评论失败', [], 500);
            }
        } catch (\Exception $e) {
            return show(config('code.error'), $e->getMessage(), [], 500);
        }

    }

    /**
     * 获取评论列表 v1
     */
//    public function read()
//    {
////        select * from ent_comment as a join ent_user as b on a.user_id=b.id and a.news_id=2;
//
//        $newsId = input('param.id', 0, 'intval');
//        if (empty($newsId)) {
//            return new ApiException('id is not', 403);
//        }
//
//        $param['news_id'] = $newsId;
//        $count = model('Comment')->getNormalCommentsCountByCondition($param);
//        $this->getPageAndSize(input('param.'));
//        $comments = model('Comment')->getNormalCommentsByCondition($param, $this->from, $this->size);
//
//        $result = [
//            'total' => $count,
//            'page_num' => ceil($count / $this->size),
//            'list' => $comments,
//        ];
//
//        return show(config('code.success'), 'ok', $result, 200);
//    }


    /**
     * 获取评论列表 v2
     * @return ApiException|array
     */
    public function read()
    {
//        select * from ent_comment as a join ent_user as b on a.user_id=b.id and a.news_id=2;

        $newsId = input('param.id', 0, 'intval');
        if (empty($newsId)) {
            return new ApiException('id is not', 403);
        }

        $param['news_id'] = $newsId;
        $count = model('Comment')->getCountByCondition($param);
        $this->getPageAndSize(input('param.'));
        $comments = model('Comment')->getListsByCondition($param, $this->from, $this->size);

        $userIds = [];
        if ($comments) {
            foreach ($comments as $comment) {
                $userIds[] = $comment['user_id'];
                if ($comment['to_user_id']) {
                    $userIds[] = $comment['to_user_id'];
                }
            }
            $userIds = array_unique($userIds);
        }
        $userIds = model('User')->getUserUserId($userIds);
        if (empty($userIds)) {
            $userIdsNames = [];
        } else {
            foreach ($userIds as $userId) {
                $userIdsNames[$userId->id] = $userId;
            }
        }
        $resultDatas = [];
        foreach ($comments as $comment) {
            $resultDatas[] = [
                'id' => $comment->id,
                'user_id' => $comment->user_id,
                'to_user_id' => $comment->to_user_id,
                'content' => $comment->content,
                'username' => !empty($userIdsNames[$comment->user_id]) ? $userIdsNames[$comment->user_id]->username : '',
                'tousername' => !empty($userIdsNames[$comment->to_user_id]) ? $userIdsNames[$comment->to_user_id]->username : '',
                'parent_id' => $comment->parent_id,
                'create_time' => $comment->create_time,
                'image' => !empty($userIdsNames[$comment->user_id]) ? $userIdsNames[$comment->user_id]->image : '',
            ];
        }


        $result = [
            'total' => $count,
            'page_num' => ceil($count / $this->size),
            'list' => $resultDatas,
        ];

        return show(config('code.success'), 'ok', $result, 200);
    }


}

